<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>

<script>
import FileUpload from "@/components/FileUpload/index";
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base],
  components: { FileUpload },
  data() {
    return {
      load: true,

      configuration: {
        mpAccount: "",
        mpName: "",
        originalId: "",
        isOpen: "",
      },

      form: {
        // AppID
        appId: "",
        // 文件
        base64File: "",
        // 证书名称
        certName: "",
        id: "",
        // 证书路径
        keyPath: "",
        // 微信支付商户号
        mchId: "",
        // 微信支付密钥
        mchKey: "",
        // 付款类型
        paymentType: "",
      },

      show: true,
      fileList: [],

      apiUrl: {
        detail: "mall-site-portal/payment-config/getItem",
        configuration: "mall-site-portal/payment-config/getWeChatMpItem",
        save: "mall-site-portal/payment-config/saveOrUpdate",
      },
    };
  },
  mounted() {},
  methods: {
    init() {
      this.fileList = [];
      this.getData();
      this.getConfiguration();
    },

    // 文件上传
    async onChange(file) {
      let fd = new FormData();
      fd.append("certFile", file.raw);

      let _data = await this.$vFN.Http.post({
        url: this.apiUrl.uploadCert,
        isQsData: false,
        data: fd,
      },window);

      if (_data._ok) {
        this.form.keyPath = _data.data;
        this.form.certName = file.name;
        this.show = true;
      }
    },
    // 文件删除
    onRemove() {
      this.form.keyPath = "";
      this.form.certName = "";
      this.show = false;
    },
    // 保存
    async submit() {
      this.load = true;
      let _data = await this.$vFN.Http.post({
        url: this.apiUrl.save,
        data: this.form,
      },window);

      if (_data._ok) {
        this.init();
        this.$vFN.Tools.messageSuccess("保存修改成功");
      }

       this.load = false;
    },

    // 获取内容信息
    async getData() {
      this.load = true;
      let _data = await this.$vFN.Http.post({
        url: this.apiUrl.detail,
        data: { paymentType: "REDPACK" },
      },window);

      if (_data._ok) {
        for (let i in this.form) {
          let _d = _data.data[i];
          if (_d != null) {
            this.form[i] = _d;
          }
        }
        this.form.keyPath == "" ? this.show = false : "";
      }

      this.load = false;
    },

    // 获取公众号配置
    async getConfiguration() {
      let _data = await this.$vFN.Http.post({
        url: this.apiUrl.configuration,
      },window);

      if (_data._ok) {
          this.configuration = {..._data.data}
      }
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>
